
package com.lange.fileManagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.lange.fileManagement.service.BaseService;
import com.lange.fileManagement.service.CaptchaService;
import com.lange.fileManagement.service.UserService;
import com.lange.fileManagement.utils.AESUtil;
import com.lange.fileManagement.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    @Autowired
    private CaptchaService captchaService;

    /*添加用户*/
    @PostMapping("/addUser")
    public Result addUser(@RequestBody JSONObject data, @RequestHeader(value = "token", defaultValue = "null") String token) throws Exception {
        // 校验验证码
        boolean validataCode = captchaService.validataCode(data.getString("captcha"));
        if (!validataCode) {
            return Result.error("100", "验证码不正确");
        }
        Integer i = userService.addUser(data);
        switch (i) {
            case 101:
                return Result.error("101", "已存在管理员");
            case 102:
                return Result.error("102", "用户名已存在");
            case 1:
                return Result.success();
            default:
                return Result.error("500", "系统出错了");
        }
    }

    /*是否存在管理员*/
    @GetMapping("/hadAdmin")
    public Result hadAdmin() {
        return Result.success(userService.hadAdmin());
    }

    /*登录*/
    @PostMapping("/login")
    public Result login(@RequestBody JSONObject data) throws Exception {
        boolean validataCode = captchaService.validataCode(data.getString("captcha"));
        if (!validataCode) {
            return Result.error("101", "验证码不正确");
        }
        String password = data.getString("password");
        String decrypt = AESUtil.decrypt(password, "lange6868qwertyu", "lange6868qwertyu");
        JSONObject login = userService.login(data);
        Integer code = login.getInteger("code");
        if (code == 0) {
            return Result.success(login.getString("token"));
        } else if (code == 100) {
            return Result.error("100", "用户名或密码错误");
        } else {
            return Result.error("101", "系统异常");
        }
    }

    /*用户个人信息*/
    @GetMapping("/getMyInfo")
    public Result getMyInfo(@RequestHeader(value = "token", defaultValue = "null") String token) {
        return Result.success(userService.getMyInfo(token));
    }

    /*找回密码*/
    @PostMapping("/findPassword")
    public Result findPassword(@RequestBody JSONObject data) throws Exception {
        // 校验验证码
        boolean validataCode = captchaService.validataCode(data.getString("captcha"));
        if (!validataCode) {
            return Result.error("101", "验证码不正确");
        }
        Integer i = userService.findPassword(data);
        switch (i) {
            case 100:
                return Result.error("100", "用户名或密保不正确");
            case 101:
                return Result.error("101", "系统异常");
            case 0:
                return Result.success();
            default:
                return Result.error("500", "系统出错了");
        }
    }

    /*检验token是否过期*/
    @GetMapping("/checkToken")
    public Result checkToken(@RequestHeader(value = "token", defaultValue = "null") String token) {
        return userService.checkToken(token);
    }
}
